package com.ruoyi.employer.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.employer.domain.Transfers;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.employer.domain.PayWechat;
import com.ruoyi.employer.service.IPayWechatService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 货主微信钱包管理Controller
 * 
 * @author ysq
 * @date 2024-08-14
 */
@RestController
@RequestMapping("/employer/payWechat")
public class PayWechatController extends BaseController
{
    @Autowired
    private IPayWechatService payWechatService;

    /**
     * 查询货主微信钱包管理列表
     */
    @PreAuthorize("@ss.hasPermi('employer:payWechat:list')")
    @GetMapping("/list")
    public TableDataInfo list(PayWechat payWechat)
    {
        startPage();
        List<PayWechat> list = payWechatService.selectPayWechatList(payWechat);
        return getDataTable(list);
    }

    /**
     * 导出货主微信钱包管理列表
     */
    @PreAuthorize("@ss.hasPermi('employer:payWechat:export')")
    @Log(title = "货主微信钱包管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PayWechat payWechat)
    {
        List<PayWechat> list = payWechatService.selectPayWechatList(payWechat);
        ExcelUtil<PayWechat> util = new ExcelUtil<PayWechat>(PayWechat.class);
        util.exportExcel(response, list, "货主微信钱包管理数据");
    }

    /**
     * 获取货主微信钱包管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('employer:payWechat:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(payWechatService.selectPayWechatById(id));
    }

    /**
     * 新增货主微信钱包管理
     */
    @PreAuthorize("@ss.hasPermi('employer:payWechat:add')")
    @Log(title = "货主微信钱包管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PayWechat payWechat)
    {
        return toAjax(payWechatService.insertPayWechat(payWechat));
    }

    /**
     * 修改货主微信钱包管理
     */
    @PreAuthorize("@ss.hasPermi('employer:payWechat:edit')")
    @Log(title = "货主微信钱包管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PayWechat payWechat)
    {
        return toAjax(payWechatService.updatePayWechat(payWechat));
    }

    /**
     * 删除货主微信钱包管理
     */
    @PreAuthorize("@ss.hasPermi('employer:payWechat:remove')")
    @Log(title = "货主微信钱包管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(payWechatService.deletePayWechatByIds(ids));
    }
    /**
     * 充值接口(加钱)
     */
    @PreAuthorize("@ss.hasPermi('employer:payWechat:edit')")
    @Log(title = "充值", businessType = BusinessType.UPDATE)
    @PutMapping("/recharge")
    public ResponseEntity<String> recharge(@RequestBody Transfers transfers) {
        System.out.println(transfers);
        double amount = transfers.getAmount();
        boolean success = payWechatService.recharge(transfers.getFromId(), amount);
        if (success) {
            return ResponseEntity.ok("充值成功");
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("充值失败");
        }
    }
    /**
     *（减钱）
     */
    @PreAuthorize("@ss.hasPermi('employer:payWechat:edit')")
    @Log(title = "给余额充值", businessType = BusinessType.UPDATE)
    @PutMapping("/withdraw")
    public AjaxResult withdraw(@RequestBody Transfers transfers) {
        System.out.println(transfers);
        double amount = transfers.getAmount();
        boolean success = payWechatService.withdraw(transfers.getComeId(), amount);
        if (success) {
            return success(true);
        } else {
            return success(false);
        }
    }
}
